import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import * as JsBarcode from 'jsbarcode';
// @ts-ignore
import QRCode from 'qrcode';
import {UsedStorePService} from '../../../../used-store-p.service';

@Component({
  selector: 'app-print-logistics',
  templateUrl: './print-logistics.component.html',
  styleUrls: ['./print-logistics.component.scss']
})
export class PrintLogisticsComponent implements OnInit, OnChanges {
  date = {
    day: '',
    time: '',
  };
  serialText = '';
  @Input() data: any = {
    xb_flag: '',
    coding_mapping: '',
    dest_team_code: '',
    pro_code: '',
    source_transfer_code: '',
    waybill_no: '',
    dest_route_label: '',
    print_icon: '',
    two_dimension_code: '',
    pro_name: '',
    address: '',
    tel: '',
    consignee: '',
    number: 0,
    payMethod: 0,
    addresser: {region: '', tel: '', contacts: ''},
  };

  constructor(
    public usedStorePService: UsedStorePService
  ) {
  }

  ngOnInit(): void {
  }

  ngOnChanges(changes: SimpleChanges) {
    const date = new Date();
    this.date = {
      // @ts-ignore
      day: date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0],
      // @ts-ignore
      time: date.toString().match(/\d{2}:\d{2}:\d{2}/)[0],
    };
    if (this.data?.waybill_no) {
      const serialText = this.data?.waybill_no;
      JsBarcode('.barcode', serialText, {
        margin: 0,
        width: 2,
        height: 50,
        displayValue: false,
      });
      // tslint:disable-next-line:max-line-length
      this.serialText = '运单号： ' + serialText.slice(0, 2) + ' ' + serialText.slice(2, 5) + ' ' + serialText.slice(5, 8) + ' ' + serialText.slice(8, 11) + ' ' + serialText.slice(11, 15);
    }
    if (this.data?.two_dimension_code) {
      QRCode.toString(this.data?.two_dimension_code, {
        type: 'terminal',
        margin: 0
      }, (err: any, url: any) => {
        // @ts-ignore
        document.getElementById('qrcode').innerHTML = url;
      });
    }
  }

}
